<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>成绩管理</title>
    <script src="./js/vue.js"></script>
    <style>
        body {
            font-family: 'Arial', sans-serif;
            background-color: #f4f4f4;
            margin: 0;
            padding: 20px;
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
        }
        .score-case {
            max-width: 600px; /* 适合手机和桌面屏幕 */
            width: 100%;
            padding: 20px;
            background-color: white;
            border-radius: 8px;
            box-shadow: 0 0 15px rgba(0, 0, 0, 0.1);
            text-align: center;
        }
        h1 {
            color: #333;
            font-size: 1.8em;
            margin-bottom: 15px;
        }
        .form {
            margin-top: 20px;
            text-align: left;
        }
        .form-item {
            margin-bottom: 15px;
        }
        label {
            display: inline-block;
            width: 60px;
            font-weight: bold;
        }
        input[type="text"] {
            width: calc(100% - 70px); /* 输入框占据剩余空间 */
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 0.9em;
        }
        button {
            background-color: #007bff; /* 蓝色按钮 */
            color: white;
            padding: 8px 15px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 0.9em;
            transition: background-color 0.3s;
        }
        button:hover {
            background-color: #0056b3; /* 悬停时的深蓝色 */
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 15px;
        }
        th, td {
            padding: 8px;
            text-align: left;
            border-bottom: 1px solid #ddd;
            font-size: 0.9em;
        }
        th {
            background-color: #f2f2f2;
            font-weight: bold;
        }
        .red {
            color: red;
        }
        tfoot td {
            font-weight: bold;
            font-size: 1.1em;
        }
    </style>
</head>

<body>
    <div id="app" class="score-case">
        <h1>成绩管理</h1>
        <div class="table">
            <table>
                <thead>
                    <tr>
                        <th>编号</th>
                        <th>科目</th>
                        <th>成绩</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                    <tr v-for="(item, index) in list" :key="item.id">
                        <td>{{ index + 1 }}</td>
                        <td>{{ item.subject }}</td>
                        <td :class="{ red: item.score < 60 }">{{ item.score }}</td>
                        <td><a href="#" @click.prevent="del(item.id)">删除</a></td>
                    </tr>
                </tbody>
                <tfoot>
                    <tr>
                        <td colspan="4">
                            总分：{{ total }} 平均分：{{ average }}
                        </td>
                    </tr>
                </tfoot>
            </table>
        </div>
        <div class="form">
            <div class="form-item">
                <label for="subject">科目：</label>
                <input type="text" id="subject" v-model.trim="subject" placeholder="请输入科目">
            </div>
            <div class="form-item">
                <label for="score">分数：</label>
                <input type="text" id="score" v-model.number="score" placeholder="请输入分数">
            </div>
            <div class="form-item">
                <button @click="add" class="submit">添加</button>
            </div>
        </div>
    </div>

    <script>
        const app = new Vue({
            el: '#app',
            data: {
                list: [
                    { id: 1, subject: '语文', score: 20 },
                    { id: 7, subject: '数学', score: 99 },
                    { id: 12, subject: '英语', score: 70 },
                ],
                subject: '',
                score: 0
            },
            computed: {
                total() {
                    return this.list.reduce((prev, item) => prev + item.score, 0);
                },
                average() {
                    if (this.list.length === 0) {
                        return 0;
                    }

                    return (this.total / this.list.length).toFixed(2);
                }
            },
            methods: {
                del(id) {
                    this.list = this.list.filter(item => item.id !== id);
                },
                add() {
                    if (!this.subject || isNaN(this.score)) {
                        alert('请确认您输入的是正确的内容');
                        return;
                    }

                    this.list.push({
                        id: +new Date(),
                        subject: this.subject,
                        score: parseInt(this.score)
                    });

                    this.subject = '';
                    this.score = 0;
                }
            }
        });
    </script>
</body>

</html>